#!/usr/bin/python
import os
import time
CheckDir="/var/spool/pbs/spool/"
FileSize=400000000
LSCommand = "ls -lS "+CheckDir +"|grep -v total | grep -v ^d"
fileList=""
fileList=os.popen(LSCommand).read()
List=fileList.split('\n')
if (List):
  for eachFile in List:
        details=eachFile.split()
        if (details):
                if (int(details[4]) > FileSize):
                    os.system("echo 'The job has generated a big log file, so it is cleaned by the system \n' >"+ CheckDir+details[8])
                    os.system("chown "+details[2]+" "+CheckDir+details[8])
                    os.system("chgrp "+details[3]+" "+CheckDir+details[8])
                    os.system("echo `date +\"%b %d %H:%m:%S \"` `hostname` "+details[4]+" file: "+CheckDir+details[8]+" was cleaned! \n"+">>/var/log/messages")

                period=(time.time()-os.path.getatime(CheckDir+details[8]))/(3600*24)
                if (period>14):
                    os.system("rm"+" "+CheckDir+details[8])
                    os.system("echo `date +\"%b %d %H:%m:%S \"` `hostname` "+details[4]+" file: "+CheckDir+details[8]+" was deleted! \n"+">>/var/log/messages")
